package com.hiberante.dao;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * CoreBusinessDetail entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hiberante.dao.CoreBusinessDetail
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class CoreBusinessDetailDAO {
	private static final Logger log = LoggerFactory
			.getLogger(CoreBusinessDetailDAO.class);
	// property constants
	public static final String BD_ORDER_ID = "bdOrderId";
	public static final String BD_PAYER_BANK_ID = "bdPayerBankId";
	public static final String BD_PAYER_USER_ID = "bdPayerUserId";
	public static final String BD_PAYER_PAY_ID = "bdPayerPayId";
	public static final String BD_PAYEE_BANK_ID = "bdPayeeBankId";
	public static final String BD_PAYEE_USER_ID = "bdPayeeUserId";
	public static final String BD_PAYEE_PAY_ID = "bdPayeePayId";
	public static final String BD_BUSINESS_STYLE_ID = "bdBusinessStyleId";
	public static final String BD_SUM_OF_MONEY = "bdSumOfMoney";
	public static final String BD_PAY_MODE = "bdPayMode";
	public static final String BD_PAY_STATUS = "bdPayStatus";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(CoreBusinessDetail transientInstance) {
		log.debug("saving CoreBusinessDetail instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CoreBusinessDetail persistentInstance) {
		log.debug("deleting CoreBusinessDetail instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CoreBusinessDetail findById(java.lang.Integer id) {
		log.debug("getting CoreBusinessDetail instance with id: " + id);
		try {
			CoreBusinessDetail instance = (CoreBusinessDetail) getCurrentSession()
					.get("com.hiberante.dao.CoreBusinessDetail", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CoreBusinessDetail instance) {
		log.debug("finding CoreBusinessDetail instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("com.hiberante.dao.CoreBusinessDetail")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CoreBusinessDetail instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CoreBusinessDetail as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByBdOrderId(Object bdOrderId) {
		return findByProperty(BD_ORDER_ID, bdOrderId);
	}

	public List findByBdPayerBankId(Object bdPayerBankId) {
		return findByProperty(BD_PAYER_BANK_ID, bdPayerBankId);
	}

	public List findByBdPayerUserId(Object bdPayerUserId) {
		return findByProperty(BD_PAYER_USER_ID, bdPayerUserId);
	}

	public List findByBdPayerPayId(Object bdPayerPayId) {
		return findByProperty(BD_PAYER_PAY_ID, bdPayerPayId);
	}

	public List findByBdPayeeBankId(Object bdPayeeBankId) {
		return findByProperty(BD_PAYEE_BANK_ID, bdPayeeBankId);
	}

	public List findByBdPayeeUserId(Object bdPayeeUserId) {
		return findByProperty(BD_PAYEE_USER_ID, bdPayeeUserId);
	}

	public List findByBdPayeePayId(Object bdPayeePayId) {
		return findByProperty(BD_PAYEE_PAY_ID, bdPayeePayId);
	}

	public List findByBdBusinessStyleId(Object bdBusinessStyleId) {
		return findByProperty(BD_BUSINESS_STYLE_ID, bdBusinessStyleId);
	}

	public List findByBdSumOfMoney(Object bdSumOfMoney) {
		return findByProperty(BD_SUM_OF_MONEY, bdSumOfMoney);
	}

	public List findByBdPayMode(Object bdPayMode) {
		return findByProperty(BD_PAY_MODE, bdPayMode);
	}

	public List findByBdPayStatus(Object bdPayStatus) {
		return findByProperty(BD_PAY_STATUS, bdPayStatus);
	}

	public List findAll() {
		log.debug("finding all CoreBusinessDetail instances");
		try {
			String queryString = "from CoreBusinessDetail";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CoreBusinessDetail merge(CoreBusinessDetail detachedInstance) {
		log.debug("merging CoreBusinessDetail instance");
		try {
			CoreBusinessDetail result = (CoreBusinessDetail) getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CoreBusinessDetail instance) {
		log.debug("attaching dirty CoreBusinessDetail instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CoreBusinessDetail instance) {
		log.debug("attaching clean CoreBusinessDetail instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CoreBusinessDetailDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CoreBusinessDetailDAO) ctx.getBean("CoreBusinessDetailDAO");
	}
}